#include "IDFSSolver.h"
IDFSSolver::IDFSSolver(Node *root) : Solver(root)
{
}

void IDFSSolver::solve(std::string order, std::string id_heuristic) {
	firstPush();
	std::string *order2;
	if (order.at(0) == 'R'){
		order2 = new std::string("LPGD");
	}
	else {
		order2 = new std::string(order);
	}
	while (!this->checkWinState()) {
		this->getNode();
		this->createChildren(order2);
		if (order.at(0) == 'R'){
			this->randomOrder(order2);
		}
		this->pushChildren();
		countNode++;
		if (maxDepth < node->getLevel()){
			maxDepth = node->getLevel();
		}
	}
	std::string steps = findPath(node);
	std::cout << steps.size() << std::endl << steps;
}

void IDFSSolver::firstPush(){
	stack.push(node);
	parent = node;
}

void IDFSSolver::getNode(){
	if (stack.empty()) {
		levelMax++;
		node = parent;
		stack.push(node);
	}
	node = stack.top();
	stack.pop();
}
bool IDFSSolver::checkVisited(){
	return false;
}

void IDFSSolver::createChildren(std::string *order){
	if (node->getLevel() == levelMax - 1) {
		for (int j = 0; j < order->size(); j++) {
			node->createChild(order->at(j));
		}
	}
}

void IDFSSolver::pushChildren(){
	if (node->getLevel() < levelMax) {
		for (int i = node->getChildren()->size() - 1; i >= 0; i--) {
			stack.push(node->getChildren()->at(i));
			countChildren++;
		}
	}
}


IDFSSolver::~IDFSSolver()
{
}


